package com.graduate.examapp.view.customview

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.NumberPicker
import android.widget.TextView
import android.widget.Toast
import com.graduate.examapp.R
import com.graduate.examapp.entity.AddUserParam
import com.graduate.examapp.entity.ExamRangeParam
import com.graduate.examapp.entity.UserInfoFactory

class CustomTeacherAddDialog(
    mContext: Context,
    val agreeCallBack: (AddUserParam) -> Unit
) :Dialog (mContext) {
    private var disAgree: TextView? = null
    private var agree: TextView? = null
    private var teacherName: CustomNormalEditText? = null
    private var teacherId: CustomNormalEditText? = null
    private var insNameTx: TextView? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        val inflate = View.inflate(context, R.layout.add_teacher_dialog, null)
        //this.window?.setBackgroundDrawable(context.getDrawable(R.drawable.dialog_back))
        this.setCanceledOnTouchOutside(false)
        this.setContentView(inflate)
        super.onCreate(savedInstanceState)
        initElement()
        initListener()
        //设置自适应的方法：
        //设置自适应的方法：
        val dialogParams: WindowManager.LayoutParams? = this.window?.attributes
        dialogParams?.width = WindowManager.LayoutParams.MATCH_PARENT
        dialogParams?.height = WindowManager.LayoutParams.WRAP_CONTENT
    }

    private fun initElement() {
        insNameTx = findViewById(R.id.add_teacher_ins_tx)
        insNameTx?.text = UserInfoFactory.loginData?.insName
        teacherName = findViewById(R.id.add_teacher_name_et)
        teacherId = findViewById(R.id.add_teacher_id_et)
        disAgree = findViewById(R.id.add_teacher_cancel)
        agree = findViewById(R.id.add_teacher_confirm)
    }

    private fun initListener() {
        disAgree?.setOnClickListener {
            this.cancel()
        }

        agree?.setOnClickListener {
            this.cancel()
            if (
                teacherId?.editText?.text.toString().isNotEmpty() &&
                !UserInfoFactory.loginData?.insName.toString().isNullOrBlank() &&
                teacherName?.editText?.text.toString().isNotEmpty()
            ) {
                val addUserParam = AddUserParam(
                    teacherId?.editText?.text.toString(),
                    teacherName?.editText?.text.toString(),
                    UserInfoFactory.loginData?.insName.toString(),
                    1,
                    "teacher"
                )
                agreeCallBack(addUserParam)
            }
            else {
                Toast.makeText(context, "请输入完成参数", Toast.LENGTH_SHORT).show()
            }
        }
    }
}